package com.redoor.app.stake.basicdata.service.impl;

import com.redoor.app.stake.basicdata.commons.BasicDataStatus;
import com.redoor.app.stake.basicdata.dao.BaseRegistrationApplyMapper;
import com.redoor.app.stake.basicdata.model.BaseRegistrationApply;
import com.redoor.app.stake.basicdata.model.RegistrationApplyForRedis;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseRegistrationApplyCriteria;
import com.redoor.app.stake.basicdata.model.serviceModel.BaseRegistrationApplyBean;
import com.redoor.app.stake.basicdata.service.BaseRegistrationApplyService;
import com.redoornetwork.framework.core.exception.ServiceException;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import com.yotouch.app.stake.model.RegistrationApply;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by wangjunlei on 2018-03-08 16:34:23.
 */
@Service
public class BaseRegistrationApplyServiceImpl extends AbstractService<BaseRegistrationApply, String> implements BaseRegistrationApplyService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private BaseRegistrationApplyMapper baseRegistrationApplyMapper;

    @Override
    public int save (RegistrationApplyForRedis registrationApplyForRedis) {
        int res = 0;
        if (null == registrationApplyForRedis){
            return res;
        }
        BaseRegistrationApply registrationApply = new BaseRegistrationApply();
        registrationApply.setUuid(registrationApplyForRedis.getUuid());
        registrationApply.setAmount(registrationApplyForRedis.getCount()); //注册码申请数量
        registrationApply.setProrgcode(registrationApplyForRedis.getOuCode()); //产权单位
        registrationApply.setStaketype(registrationApplyForRedis.getType());   //充电桩类型
        registrationApply.setRemark(registrationApplyForRedis.getNote());  //申请备注
        registrationApply.setId(registrationApplyForRedis.getOrderId());   //运维系统传递来的编号，用于后续注册码查询
        registrationApply.setVendorname(registrationApplyForRedis.getVendorName());    //生产厂家
        registrationApply.setEquipmodel(registrationApplyForRedis.getEquipModel());    //设备型号
        registrationApply.setChargerfeemodelcode(registrationApplyForRedis.getChargerFeeModelId());    //电费模型
        registrationApply.setServicefeemodelcode(registrationApplyForRedis.getServiceFeeModelId());    //服务费模型
        registrationApply.setCreatoruuid(registrationApplyForRedis.getCreatorUuid());   //创建人
        registrationApply.setCreatedat(registrationApplyForRedis.getCreatedAt());// 创建时间

        registrationApply.setStatus(Integer.parseInt(BasicDataStatus.WORKORDER_STATUS_APPLYING.getCode()));    //状态 审核中
        res = baseRegistrationApplyMapper.insert(registrationApply);
        logger.info("BaseRegistrationApplyServiceImpl_save_success_registrationApplyUuid :"+registrationApply.getUuid());
        return res;
    }

    @Override
    public int saveAll(List<RegistrationApplyForRedis> registrationApplyForRedisList) {
        if(null != registrationApplyForRedisList && 0 < registrationApplyForRedisList.size()){
            for (RegistrationApplyForRedis registrationApplyForRedis : registrationApplyForRedisList){
                // TODO: 2018/4/3 0003 zhangmumu 注册码信息从redis里面批量取出处理,是不是要增加日志
                save(registrationApplyForRedis);
            }
            return 1;
        }else{
            return 0;
        }
    }
    @Override
    public BaseRegistrationApply getBaseRegistrationApply(String uuid) {

        if (StringUtils.isBlank(uuid)){

            return null;
        }
        // TODO 可以增加redis缓存
        BaseRegistrationApply baseRegistrationApply = baseRegistrationApplyMapper.selectByPrimaryKey(uuid);
        return baseRegistrationApply;
    }

    @Override
    public List<BaseRegistrationApply> queryRegistrationApply(BaseRegistrationApplyCriteria criteria) {
        List<BaseRegistrationApply> applyList = baseRegistrationApplyMapper.queryRegistrationApply(criteria);
        return applyList;
    }

    @Override
    public RegistrationApply getHsfRegistrationApply(String uuid, String status) {
        BaseRegistrationApplyCriteria criteria = new BaseRegistrationApplyCriteria();
        criteria.setUuid(uuid);
        criteria.setStatus(status);

        RegistrationApply registrationApply = baseRegistrationApplyMapper.getHsfRegistrationApply(criteria);
        return registrationApply;
    }

    @Override
    public BaseRegistrationApplyBean detail(String uuid) {
        if(StringUtils.isBlank(uuid)){
            return null;
        }
        return baseRegistrationApplyMapper.detail(uuid);
    }
}
